package com.bamboo.eduservice.controller;


import com.bamboo.commonutils.ResponseEntity;
import com.bamboo.eduservice.entity.EduCourse;
import com.bamboo.eduservice.entity.vo.CourseInfoVo;
import com.bamboo.eduservice.entity.vo.CoursePublishVo;
import com.bamboo.eduservice.entity.vo.CourseQuery;
import com.bamboo.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author jack.wu
 * @since 2020-07-05
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Resource
    private EduCourseService courseService;

    @ApiOperation(value = "添加课程信息")
    @PostMapping("add")
    public ResponseEntity addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String cid = courseService.addCourseInfo(courseInfoVo);
        return ResponseEntity.ok().data("courseId", cid);
    }

    @ApiOperation(value = "查询课程信息")
    @GetMapping("info/{courseId}")
    public ResponseEntity getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfoById(courseId);
        return ResponseEntity.ok().data("courseInfo", courseInfoVo);
    }

    @ApiOperation(value = "修改课程信息")
    @PutMapping("update")
    public ResponseEntity updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return ResponseEntity.ok();
    }


    @ApiOperation(value = "查询课程发布信息")
    @GetMapping("getPublishInfo/{courseId}")
    public ResponseEntity getPublishInfo(@PathVariable String courseId) {
        CoursePublishVo publishInfo = courseService.getPublishInfo(courseId);
        return ResponseEntity.ok().data("publishInfo", publishInfo);
    }

    @ApiOperation(value = "发布课程")
    @PutMapping("publish/{courseId}")
    public ResponseEntity publish(@PathVariable String courseId) {
        courseService.publish(courseId);
        return ResponseEntity.ok();
    }

    @ApiOperation(value = "查询课程列表")
    @PostMapping("list/{current}/{limit}")
    public ResponseEntity getCourseList(@PathVariable long current, @PathVariable long limit,
                                        @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> records = courseService.getCourseList(current, limit, courseQuery);
        return ResponseEntity.ok().data("total", records.getTotal()).data("rows", records.getRecords());
    }

    @ApiOperation(value = "删除课程")
    @GetMapping("delete/{courseId}")
    public ResponseEntity deleteCourseInfo(@PathVariable String courseId) {
        courseService.deleteCourseInfo(courseId);
        return ResponseEntity.ok();
    }


}


